/////////
// Replication syntax for obervational analyses of the paper 
// Vierus & Ziller (2025) - Political support in times of progressive policy change and radical-right populist party success
///////// 

	version 17.0
	set more off
	clear all 

//intalling ados
local adolist fre rev

foreach ado in `adolist' {
    cap which `ado'
    if _rc != 0 {
        di "`ado' is not installed. Installing now..."
        ssc install `ado'
    }
    else {
        di "`ado' is already installed."
		}
}
	

*Setup working directory	
cd "WORKING DIRECTORY"
	
//Merging ESS datasets
************************************************************************************
//ESS5	
use "ESS5.dta", clear	
gen ess5 = 1 

qui do "ESS5_regsupport_solo.do" 	//merge of support for incumbent government
save "ESS5_mod.dta", replace

//ESS7
use "ESS7.dta", clear	
gen ess7 = 1 

qui do "ESS7_regsupport_solo.do" 	//merge of support for incumbent government
save "ESS7_mod.dta", replace

//ESS9
use "ESS9.dta", clear	
gen ess9 = 1 

qui do "ESS9_regsupport_solo.do" 	//merge of support for incumbent government
append using  "ESS5_mod.dta"
append using  "ESS7_mod.dta"

//Missings deklarieren
do "ESS9e03_1_formats_unicode.do"

//Creating year
gen year = . 
replace year = 2010 if ess5 == 1
replace year = 2014 if ess7 == 1
replace year = 2018 if ess9 == 1

lab var year "Year"
rename cntry cntry_2      


//Preparing indivudual variables
****************************************	
//suppport for incumbent government
tab  vote reg_support, m
replace reg_support = 0 if vote == 2 | vote == 3 
replace reg_support = 0 if vote == 1 & reg_support != 1

//political trust
alpha trstprl trstplt trstprt, item gen(poltrust_3)	

cap drop right	
gen right = 0 if inlist(lrscale,0,1,2,3,4,5,6,7,8,9,10)
replace right = 1 if lrscale > 5	

//education 
recode eduyrs (0/15 = 1 "Up to 15 years") (16/19=2 "16-19") (20/60=3 "20 years and more") , gen(edu)
replace edu = 4 if edctn == 1
lab define edu 4 "Still Studying", add
rename eisced edu_isced

//age
gen alter = agea 
drop agea
rename alter age

//sex
recode gndr (1=0 "male") (2=1 "female"), gen(sex)

//occupation
recode mnactic (1 = 1 "employed") (3 4 = 2 "Unemployed") (2 8 = 3 "Housework/Education") (5 6 = 4 "Retired/Sick") (7 9 =.), gen(occupation)

//income
rename hincfel income 
rev income, replace				

//urban
fre domicil
recode domicil (1 2=3 "Cities/ large urban area") (3  = 2 "Towns and suburbs / small urban area") (4 5 = 1 "Rural area"), gen(urban)	

gen land2 = 1 if inlist(domicil,4,5)
replace land2 = 0 if inlist(domicil, 1,2,3)

//Lrscale
fre lrscale

//migration background
gen migr = .
replace migr = 1 if mocntr == 2 | facntr == 2 | brncntr == 2 | blgetmg == 1	
replace migr = 0 if mocntr == 1 & facntr == 1 & brncntr == 1 & blgetmg == 2


//Schwartz-Values
//Dimension Conservation
*Conformity
	rename ipfrule x_c_1
	rename ipbhprp x_c_2
*Tradition
	rename ipmodst x_t_1
	rename imptrad x_t_2
*Security
	rename impsafe x_s_1
	rename ipstrgv x_s_2		

//Dimension Salf-Transcendence
*Benevolence
	rename iphlppl x_b_1
	rename iplylfr x_b_2
*Universalism
	rename ipeqopt x_u_1
	rename ipudrst x_u_2
	rename impenv x_u_3

//Dimension Openess to Change
*Self-Direction
	rename ipcrtiv x_sd_1
	rename impfree x_sd_2
*Stimulation
	rename impdiff x_st_1
	rename ipadvnt x_st_2

//Dimension Self-Enhancement
*Achievement
	rename ipshabt x_a_1
	rename ipsuces x_a_2
*Power
	rename imprich x_p_1
	rename iprspot x_p_2

//Dimension Hedonism
*Hedonism 
	rename ipgdtim x_h_1
	rename impfun x_h_2


//2. Step: Recode
//Missings 
mvdecode x_*, mv(7 8 9)

//Missings counting
egen miss = rowmiss(x_*)

//Calculating consistency across answers
foreach num of numlist 1/6  {
	egen count`num' = anycount(x_*), values(`num')
}

//Counting variance
egen count_max = rowmax(count1 count2 count3 count4 count5 count6)

//drop cases with more than 5 missings
drop if miss > 5 |  count_max >16

// Reverse coding 
label define values ///
	1 "Not Like me at all" ///
	2 "Not Like Me " ///
	3 "A little like me " ///
	4 "Somewhat like me " ///
	5 "Like me " ///
	6 "Very Much like Me " 

foreach x of varlist x_* {
	sum `x'
	replace `x' = (r(max)+1) - `x'
	label values `x' values
}

//3. Calculating values and normalization on the individual mean
alpha x_h_1 x_h_2, gen(hedonism) 
alpha x_p_1 x_p_2, gen(power)
alpha x_a_1 x_a_2, gen(achievment)
alpha x_st_1 x_st_2, gen(stimulation)
alpha x_sd_1 x_sd_2, gen(selfdirection)
alpha x_u_1 x_u_2 x_u_3, item gen(universalism)
alpha x_b_1 x_b_2, item gen(benevolence)
alpha x_s_1 x_s_2, item gen(security)
alpha x_t_1 x_t_2, item gen(tradition)
alpha x_c_1 x_c_2, item gen(conformity)

	
**# Adding Contextual variabels
*******************************************************************	
merge m:1 cntry_2 year using "VZ_context_variables.dta",	keep(1 3) nogen


save "VZ_observational_data.dta", replace	

	
	
	
		